import models
import os
import sys
import utils
import xmmsclient
import xmmsqt4
from PyQt4 import QtCore, QtGui
from constants import *
from mlib_ui import Ui_MainWindow
from tagger_ui import Ui_Form



class Tagger(Ui_Form, QtGui.QWidget):
    def __init__(self, mlib):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
        self.mlib = mlib

    def dynamicRetag(self):
        pattern = str(self.patternEdit.text())
        if not pattern: return
        if self.titleButton.isChecked(): col = TITLE
        elif self.albumButton.isChecked(): col = ALBUM
        elif self.artistButton.isChecked(): col = ARTIST
        for row in self.mlib.selectedRows():
            data = str(self.mlib.data(row, col))
            id = long(self.mlib.data(row, ID))
            utils.retag(pattern, id, data)
        #self.close()

    def staticRetag(self):
        artist = self.artistEdit.text()
        album = self.albumEdit.text()
        print "artist", artist
        print "album", album
        for row in self.mlib.selectedRows():
            print self.mlib.data(row, TITLE)
        #self.close()


class Mlib(Ui_MainWindow, QtGui.QMainWindow):
    def __init__(self, xmms2):
        QtGui.QMainWindow.__init__(self)
        self.xmms2 = xmms2
        self.setupUi(self)
        self.artistModel = models.ArtistModel(xmms2)
        self.albumModel = models.AlbumModel(xmms2)
        self.tagsModel = models.TagsModel(xmms2)
        self.tagsProxy = QtGui.QSortFilterProxyModel()
        self.tagsProxy.setSourceModel(self.tagsModel)
        self.tagsProxy.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
        self.tagsProxy.setFilterKeyColumn(-1)
        self.artistView.setModel(self.artistModel)
        self.albumView.setModel(self.albumModel)
        self.tagsView.setModel(self.tagsProxy)
        self.tagsView.setColumnHidden(ID, True)
        self.lineEdit.connect(self.lineEdit,
                              QtCore.SIGNAL("textChanged(QString)"),
                              self.tagsProxy.setFilterFixedString)
        self.tagsView.connect(self.tagsView,
                              QtCore.SIGNAL("activated(QModelIndex)"),
                              self.tagsModel.rowActivated)

    def addSelToPlaylist(self):
        for index in self.tagsView.selectionModel().selectedRows():
            modelIndex = self.tagsProxy.mapToSource(index)
            _id = self.tagsModel.item(modelIndex.row(), ID).text()
            id = int(_id)
            self.xmms2.playlist_add_id(id)

    def batchRetag(self):
        self.tagger = Tagger(self)
        self.tagger.show()

    def data(self, row, col):
        return self.tagsModel.item(row, col).text()
        
    def selectedRows(self):
        """Returns an iterator over the selected rows
        """
        for index in self.tagsView.selectionModel().selectedRows():
            modelIndex = self.tagsProxy.mapToSource(index)
            row = modelIndex.row()
            yield row

    def setAlbumFilter(self, index):
        if index.row() == 0:
            self.tagsProxy.setFilterFixedString("")
            return
        albumName = self.albumModel.data(index).toString()
        self.tagsProxy.setFilterKeyColumn(1)
        self.tagsProxy.setFilterFixedString(albumName)
        self.tagsProxy.setFilterKeyColumn(-1)
         
    def setArtistFilter(self, index):
        if index.row() == 0:
            self.tagsProxy.setFilterFixedString("")
            return
        artistName = self.artistModel.data(index).toString()
        self.tagsProxy.setFilterKeyColumn(2)
        self.tagsProxy.setFilterFixedString(artistName)
        self.tagsProxy.setFilterKeyColumn(-1)


if __name__ == "__main__":
    xmms2 = xmmsclient.XMMS("alibaba")
    try:
        xmms2.connect(os.getenv("XMMSPATH"))
    except IOError, detail:
        print "Could not connect to xmms2!", detail
        exit(1)

    app = QtGui.QApplication(sys.argv)
    xmmsqt4.XMMSConnector(xmms2, app)    
    mlib = Mlib(xmms2)
    mlib.show()
    sys.exit(app.exec_())
    
